package com.fita.wechat.entity.search;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

public class CarLocationSearch implements QueryBuilder{
	
	private String country;
	
    private String brand;
    
    private String code;
    
    private Boolean hot;
    

    private QueryFilter queryFilter;

    private QueryOrder queryOrder;

	/* (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
	 */
	public QueryFilter getPreparedFilter(boolean refresh) {
		
        if (queryFilter == null || refresh) {

            queryFilter = new QueryFilter();

            if (country.contains(",")) {
            	
				String[] countries = country.split(",");
				
                queryFilter.addSentence(" ( country IN (:country) ) ", "country", countries);
			}else {
	            queryFilter.addEquals("country", country);
			}
            queryFilter.addEquals("supplier", brand);
            
            queryFilter.addEquals("hot", hot);
            
            if (StringUtils.isNotBlank(code)) {
				
            	code = "%" + code + "%";
            	
                queryFilter.addSentence(" ( name LIKE :code OR cityName LIKE :code OR airportCode LIKE :code ) ", "code", code);
			}
        }
        return queryFilter;
	}

	/* (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
	 */
	public QueryOrder getOrder() {
		
		if (queryOrder == null) {
			
		    queryOrder = new QueryOrder();
			
			queryOrder.add("airportIndicator", "DESC");
		}
		return queryOrder;
	}

	/* (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
	 */
	public String buildSentenceTemplate() {
        return "FROM Location WHERE disabled = false" + getPreparedFilter(false).clauseSentence() + getOrder().sentence();
	}

	/* (non-Javadoc)
	 * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
	 */
	public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
	}

	/**
	 * @return the country
	 */
	public String getCountry() {
		return country;
	}

	/**
	 * @param country the country to set
	 */
	public void setCountry(String country) {
		this.country = country;
	}

	/**
	 * @return the brand
	 */
	public String getBrand() {
		return brand;
	}

	/**
	 * @param brand the brand to set
	 */
	public void setBrand(String brand) {
		this.brand = brand;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return the hot
	 */
	public Boolean getHot() {
		return hot;
	}

	/**
	 * @param hot the hot to set
	 */
	public void setHot(Boolean hot) {
		this.hot = hot;
	}
	
}
